package com.woniu.controller.medicalService;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.model.gh.common.Result;
import com.woniu.model.gh.utils.PageVo;
import com.woniu.model.gh.utils.TypeVo;
import com.woniu.service.medicalService.IHospitalService;
import com.woniu.model.medicalService.entity.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

//医院表
@RestController
@RequestMapping("/ms_hospital")
public class HospitalController {

    @Autowired
    private IHospitalService hospitalService;

    //分页 分词 条件查询(医院名称模糊查询 医院类型 医院等级)
    @PostMapping("/findPageByCondition")
    public Result<PageVo<Hospital>> findPageByCondition(@RequestBody Map<String, Object> condition) {
        PageVo<Hospital> pageVo = hospitalService.findByName(condition);
        return Result.ok(pageVo);
    }

    //获取医院类型
    @GetMapping("/getType")
    public Result<List<TypeVo>> getType() {
        List<TypeVo> list = hospitalService.getType();
        return Result.ok(list);
    }

    //获取医院等级
    @GetMapping("/getGrade")
    public Result<List<TypeVo>> getGrade() {
        List<TypeVo> list = hospitalService.getGrade();
        return Result.ok(list);
    }

    //添加医院
    @PostMapping("/save")
    public Result<Void> save(@RequestBody Hospital hospital) {
        hospitalService.save(hospital);
        return Result.ok();
    }

    //获取添加后的最后一页
    @GetMapping("/getLastPage")
    public Result<Integer> getLastPage() {
        int lastPage = hospitalService.getLastPage();
        return Result.ok(lastPage);
    }

    //更改上下架
    @PostMapping("/changeBoole")
    public Result<Void> changeBoole(@RequestBody Hospital hospital) {
        if (hospital.getBoole() == 0L)
            hospital.setBoole(1L);
        else hospital.setBoole(0L);

        hospitalService.updateById(hospital);
        return Result.ok();
    }

    //批量更改上下架
    @PostMapping("/batchChangeBoole/{boole}")
    public Result<Void> batchChangeBoole(@RequestBody Long[] ids, @PathVariable Long boole) {
        List<Long> idList = new ArrayList<>();
        Collections.addAll(idList, ids);
        List<Hospital> hospitalList = hospitalService.listByIds(idList);
        if (boole == 0L)
            hospitalList.forEach(n -> {
                n.setBoole(0L);
                hospitalService.updateById(n);
            });
        else {
            hospitalList.forEach(n -> {
                n.setBoole(1L);
                hospitalService.updateById(n);
            });
        }
        return Result.ok();
    }

    //批量删除删除医院
    @PostMapping("/batchDelete")
    public Result<Void> batchDelete(@RequestBody Long[] ids) {
        List<Long> idList = new ArrayList<>();
        Collections.addAll(idList, ids);
        hospitalService.removeBatchByIds(idList);
        return Result.ok();
    }

    //根据id查询
    @GetMapping("/findById")
    public Result<Hospital> findById(Long hospitalId){
        Hospital hospital = hospitalService
                .getOne(Wrappers.lambdaQuery(Hospital.class)
                        .eq(Hospital::getId, hospitalId));
        return Result.ok(hospital);
    }

}
